package com.common;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class StringProcess {

	/**
	 * 将对象转换成字符
	 * @param input
	 * @return
	 */
	public static String convertToString(Object input) {
		String ret = "";
		if (input != null) {
			ret = input.toString();
		}

		return ret;
	}
	
	/**
	 * 获取URL参数并加工处理
	 * @param request
	 * @param defaultValue
	 * @return
	 */
	public static String getRequest(Object request,String defaultValue) {
		
		if(request!=null)
		{
			return request.toString();
		}
		else
		{
			return defaultValue;
		}

	}
	
	public static String getUUID()
	{
		String uuid = UUID.randomUUID().toString(); 
		uuid=uuid.replaceAll("-", "");
		
		Date currentTime= new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		
		return dateFormat.format(currentTime)+uuid;
	}
	
	//首字母转小写
	public static String toLowerCaseFirstOne(String s){
	  if(Character.isLowerCase(s.charAt(0)))
	    return s;
	  else
	    return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	//首字母转大写
	public static String toUpperCaseFirstOne(String s){
	  if(Character.isUpperCase(s.charAt(0)))
	    return s;
	  else
	    return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}
	
	//解码
	public static String decode(String input)
	{
		input=URLDecoder.decode(input);
		input=input.replace("%26", "&");
		input=input.replace("%2B", "+");
		return input;
	}
	
	//转码
	public static String encode(String input)
	{
		input=URLEncoder.encode(input);
		input=input.replace("+", "%20");
		
		return input;
	}
	
}
